package org.exez.modules.visitor.service.impl;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.exez.common.handler.IFillRuleHandler;
import org.exez.common.system.vo.LoginUser;
import org.exez.modules.system.entity.SysUser;
import org.exez.modules.system.service.ISysUserService;
import org.exez.modules.visitor.entity.Institution;
import org.exez.modules.visitor.entity.VisitorAccessre;
import org.exez.modules.visitor.entity.VisitorAccessreOld;
import org.exez.modules.visitor.mapper.VisitorAccessreMapper;
import org.exez.modules.visitor.service.IInstitutionService;
import org.exez.modules.visitor.service.IReservationApplicationService;
import org.exez.modules.visitor.service.IVisitorAccessreService;
import org.exez.modules.visitor.util.*;
import org.exez.modules.visitor.vo.AccessRecordVo;
import org.exez.modules.visitor.vo.AccessTimeVo;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 出入记录表
 * @Author: exez-boot
 * @Date: 2020-06-10
 * @Version: V1.0
 */
@Service
public class VisitorAccessreServiceImpl extends ServiceImpl<VisitorAccessreMapper, VisitorAccessre> implements IVisitorAccessreService {

    @Autowired
    VisitorAccessreMapper visitorAccessreMapper;

    @Autowired
    VisitorAccessreOldServiceImpl visitorAccessreOldService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IReservationApplicationService iReservationApplicationService;

    @Autowired
    private IInstitutionService iInstitutionService;
    @Value("${url.mogo-upload}")
    private String uploadUrl;

    @Value("${url.mogo-deleted}")
    private String deletedUrl;

    @Override
    public IPage<VisitorAccessre> listVisitorAccessre(Page<VisitorAccessre> page, LambdaQueryWrapper<VisitorAccessre> queryWrapper) {
        IPage<VisitorAccessre> pageList = this.page(page, queryWrapper);
        return pageList;
    }

    @Override
    public List<Object> carData(int type, String institutionId) {
        Map<String, Object> map = new HashMap<>();

        map.put("success", true);
        map.put("message", "操作成功!");
        map.put("code", "200");
        List<Object> result = new ArrayList<>();
        String now = DateUtil.getTime();
        //周
        Map<String, Object> dt = null;
        AccessTimeVo accessTimeVo = new AccessTimeVo();
        //LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //数据权限
        String area = institutionId;


        if (StringUtils.isNotEmpty(area) && !"null".equals(institutionId)) {
            if (area.length() > 0 && ",".equals(area.substring(area.length() - 1))) {
                area = area.substring(0, area.length() - 1);
            }
        } else {
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            area = sysUser.getInstitutionId();
            if (!StringUtils.isNotEmpty(area)) {
                area = "";
            }
            if (area.length() > 0 && ",".equals(area.substring(area.length() - 1))) {
                area = area.substring(0, area.length() - 1);
            }
        }

        if (StringUtils.isNotEmpty(area)) {
            accessTimeVo.setAreaCode("(" + area + ")");
        } else {
            accessTimeVo.setAreaCode("");

        }
        if (type == 1) {

            accessTimeVo.setSt11(" 18:");
            accessTimeVo.setSt10(" 07:");
            accessTimeVo.setSt9(" 16:");
            accessTimeVo.setSt8(" 15:");
            accessTimeVo.setSt7(" 14:");
            accessTimeVo.setSt6(" 13:");
            accessTimeVo.setSt5(" 12:");
            accessTimeVo.setSt4(" 11:");
            accessTimeVo.setSt3(" 10:");
            accessTimeVo.setSt2(" 09:");
            accessTimeVo.setSt1(" 08:");

            accessTimeVo.setSt12(DateUtil.getQianXday(7).substring(0, 10) + " 00:00:00");
            accessTimeVo.setEnd12(DateUtil.getQianXday(1).substring(0, 10) + " 23:59:59");
            Map<String, String> map1 = visitorAccessreMapper.selectHourTimeByCar(accessTimeVo);
            Map<String, String> map2 = visitorAccessreMapper.selectHourTimeByCarOut(accessTimeVo);
            for (int i = 1; i < 12; i++) {
                dt = new HashMap<>();
                if (map1 != null) {
                    dt.put("white", map1.get("t" + i));
                } else {
                    dt.put("white", "0");
                }
                if (map2 != null) {
                    dt.put("notWhite", map2.get("t" + i));
                } else {
                    dt.put("notWhite", "0");
                }
                if (i > 2) {
                    dt.put("time", (i + 7) + "" + ":00");
                } else {
                    dt.put("time", "0" + (i + 7) + "" + ":00");
                }

                result.add(dt);
            }

        }

        //月
        if (type == 2) {

            String x1 = DateUtil.getQianXday(1).substring(0, 10);
            String x2 = DateUtil.getQianXday(2).substring(0, 10);
            String x3 = DateUtil.getQianXday(3).substring(0, 10);
            String x4 = DateUtil.getQianXday(4).substring(0, 10);
            String x5 = DateUtil.getQianXday(5).substring(0, 10);
            String x6 = DateUtil.getQianXday(6).substring(0, 10);
            String x7 = DateUtil.getQianXday(7).substring(0, 10);
            String x8 = DateUtil.getQianXday(8).substring(0, 10);
            String x9 = DateUtil.getQianXday(9).substring(0, 10);
            String x10 = DateUtil.getQianXday(10).substring(0, 10);
            String x11 = DateUtil.getQianXday(11).substring(0, 10);
            String x12 = DateUtil.getQianXday(12).substring(0, 10);
            String x13 = DateUtil.getQianXday(13).substring(0, 10);
            String x14 = DateUtil.getQianXday(14).substring(0, 10);
            String x15 = DateUtil.getQianXday(15).substring(0, 10);
            String x16 = DateUtil.getQianXday(16).substring(0, 10);
            String x17 = DateUtil.getQianXday(17).substring(0, 10);
            String x18 = DateUtil.getQianXday(18).substring(0, 10);
            String x19 = DateUtil.getQianXday(19).substring(0, 10);
            String x20 = DateUtil.getQianXday(20).substring(0, 10);
            String x21 = DateUtil.getQianXday(21).substring(0, 10);
            String x22 = DateUtil.getQianXday(22).substring(0, 10);
            String x23 = DateUtil.getQianXday(23).substring(0, 10);
            String x24 = DateUtil.getQianXday(24).substring(0, 10);
            String x25 = DateUtil.getQianXday(25).substring(0, 10);
            String x26 = DateUtil.getQianXday(26).substring(0, 10);
            String x27 = DateUtil.getQianXday(27).substring(0, 10);
            String x28 = DateUtil.getQianXday(28).substring(0, 10);
            String x29 = DateUtil.getQianXday(29).substring(0, 10);
            String x30 = DateUtil.getQianXday(30).substring(0, 10);
            String x31 = DateUtil.getQianXday(30).substring(0, 10);
            accessTimeVo.setSt31(x31 + " 00:00:00");
            accessTimeVo.setEnd31(x31 + " 23:59:59");
            accessTimeVo.setSt30(x30 + " 00:00:00");
            accessTimeVo.setEnd30(x30 + " 23:59:59");
            accessTimeVo.setSt29(x29 + " 00:00:00");
            accessTimeVo.setEnd29(x29 + " 23:59:59");
            accessTimeVo.setSt28(x28 + " 00:00:00");
            accessTimeVo.setEnd28(x28 + " 23:59:59");
            accessTimeVo.setSt27(x27 + " 00:00:00");
            accessTimeVo.setEnd27(x27 + " 23:59:59");
            accessTimeVo.setSt26(x26 + " 00:00:00");
            accessTimeVo.setEnd26(x26 + " 23:59:59");
            accessTimeVo.setSt25(x25 + " 00:00:00");
            accessTimeVo.setEnd25(x25 + " 23:59:59");
            accessTimeVo.setSt24(x24 + " 00:00:00");
            accessTimeVo.setEnd24(x24 + " 23:59:59");
            accessTimeVo.setSt23(x23 + " 00:00:00");
            accessTimeVo.setEnd23(x23 + " 23:59:59");
            accessTimeVo.setSt22(x22 + " 00:00:00");
            accessTimeVo.setEnd22(x22 + " 23:59:59");
            accessTimeVo.setSt21(x21 + " 00:00:00");
            accessTimeVo.setEnd21(x21 + " 23:59:59");
            accessTimeVo.setSt20(x20 + " 00:00:00");
            accessTimeVo.setEnd20(x20 + " 23:59:59");


            accessTimeVo.setSt19(x19 + " 00:00:00");
            accessTimeVo.setEnd19(x19 + " 23:59:59");
            accessTimeVo.setSt18(x18 + " 00:00:00");
            accessTimeVo.setEnd18(x18 + " 23:59:59");
            accessTimeVo.setSt17(x17 + " 00:00:00");
            accessTimeVo.setEnd17(x17 + " 23:59:59");
            accessTimeVo.setSt16(x16 + " 00:00:00");
            accessTimeVo.setEnd16(x16 + " 23:59:59");
            accessTimeVo.setSt15(x15 + " 00:00:00");
            accessTimeVo.setEnd15(x15 + " 23:59:59");
            accessTimeVo.setSt14(x14 + " 00:00:00");
            accessTimeVo.setEnd14(x14 + " 23:59:59");
            accessTimeVo.setSt13(x13 + " 00:00:00");
            accessTimeVo.setEnd13(x13 + " 23:59:59");
            accessTimeVo.setSt12(x12 + " 00:00:00");
            accessTimeVo.setEnd12(x12 + " 23:59:59");
            accessTimeVo.setSt11(x11 + " 00:00:00");
            accessTimeVo.setEnd11(x11 + " 23:59:59");
            accessTimeVo.setSt10(x10 + " 00:00:00");
            accessTimeVo.setEnd10(x10 + " 23:59:59");


            accessTimeVo.setSt9(x9 + " 00:00:00");
            accessTimeVo.setEnd9(x9 + " 23:59:59");
            accessTimeVo.setSt8(x8 + " 00:00:00");
            accessTimeVo.setEnd8(x8 + " 23:59:59");
            accessTimeVo.setSt7(x7 + " 00:00:00");
            accessTimeVo.setEnd7(x7 + " 23:59:59");
            accessTimeVo.setSt6(x6 + " 00:00:00");
            accessTimeVo.setEnd6(x6 + " 23:59:59");
            accessTimeVo.setSt5(x5 + " 00:00:00");
            accessTimeVo.setEnd5(x5 + " 23:59:59");
            accessTimeVo.setSt4(x4 + " 00:00:00");
            accessTimeVo.setEnd4(x4 + " 23:59:59");
            accessTimeVo.setSt3(x3 + " 00:00:00");
            accessTimeVo.setEnd3(x3 + " 23:59:59");
            accessTimeVo.setSt2(x2 + " 00:00:00");
            accessTimeVo.setEnd2(x2 + " 23:59:59");
            accessTimeVo.setSt1(x1 + " 00:00:00");
            accessTimeVo.setEnd1(x1 + " 23:59:59");

            Map<String, String> map1 = visitorAccessreMapper.selectTimeByCar(accessTimeVo);
            Map<String, String> map2 = visitorAccessreMapper.selectTimeByCarOut(accessTimeVo);

            for (int i = 31; i > 0; i--) {
                dt = new HashMap<>();
                if (map1 != null) {
                    dt.put("white", map1.get("t" + i));
                } else {
                    dt.put("white", "0");
                }
                if (map2 != null) {
                    dt.put("notWhite", map2.get("t" + i));
                } else {
                    dt.put("notWhite", "0");
                }
                dt.put("time", DateUtil.getQianXday(i).substring(5, 10));
                result.add(dt);
            }
        }

        if (type == 3) {
            String x1 = DateUtil.getQianMonth(1).substring(0, 10);
            String x2 = DateUtil.getQianMonth(2).substring(0, 10);
            String x3 = DateUtil.getQianMonth(3).substring(0, 10);
            String x4 = DateUtil.getQianMonth(4).substring(0, 10);
            String x5 = DateUtil.getQianMonth(5).substring(0, 10);
            String x6 = DateUtil.getQianMonth(6).substring(0, 10);
            String x7 = DateUtil.getQianMonth(7).substring(0, 10);
            String x8 = DateUtil.getQianMonth(8).substring(0, 10);
            String x9 = DateUtil.getQianMonth(9).substring(0, 10);
            String x10 = DateUtil.getQianMonth(10).substring(0, 10);
            String x11 = DateUtil.getQianMonth(11).substring(0, 10);
            String x12 = DateUtil.getQianMonth(12).substring(0, 10);

            accessTimeVo.setSt12(x1.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd12(now.substring(0, 7) + "-01 00:00:00");


            accessTimeVo.setSt11(x2.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd11(x1.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setSt10(x3.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd10(x2.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setSt9(x4.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd9(x3.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setSt8(x5.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd8(x4.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setSt7(x6.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd7(x5.substring(0, 7) + "-01 00:00:00");


            accessTimeVo.setSt6(x7.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd6(x6.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setSt5(x8.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd5(x7.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setSt4(x9.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd4(x8.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setSt3(x10.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd3(x9.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setSt2(x11.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd2(x10.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setSt1(x12.substring(0, 7) + "-01 00:00:00");
            accessTimeVo.setEnd1(x11.substring(0, 7) + "-01 00:00:00");

            Map<String, String> map1 = visitorAccessreMapper.selectTimeByCar(accessTimeVo);
            Map<String, String> map2 = visitorAccessreMapper.selectTimeByCarOut(accessTimeVo);

            for (int i = 1; i < 13; i++) {
                dt = new HashMap<>();
                if (map1 != null) {
                    dt.put("white", map1.get("t" + i));
                } else {
                    dt.put("white", "0");
                }
                if (map2 != null) {
                    dt.put("notWhite", map2.get("t" + i));
                } else {
                    dt.put("notWhite", "0");
                }
                dt.put("time", DateUtil.getQianMonth(12 - (i-1)).substring(0, 7).replaceAll("-", "/"));
                result.add(dt);
            }
        }
        return result;
    }

    @Override
    public int toDayCarCount(List<Integer> company, Integer type) {
        LambdaQueryWrapper<VisitorAccessre> queryWrapper = new LambdaQueryWrapper<>();

        if (null != company) {
            queryWrapper.in(VisitorAccessre::getInstitutionCode, company);
        }
        queryWrapper.eq(VisitorAccessre::getAccessType, type);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public String insertVisitorAccess(AccessRecordVo accessRecordVo) {
        System.out.println(accessRecordVo.toString());
        String code = "";
        try {
            String dh = AESSJY.AESjiemi(accessRecordVo.getMobile().toString());
            accessRecordVo.setMobile(AES.AESjiami(dh));
            String carNo = AESSJY.AESjiemi(accessRecordVo.getCarNo().toString());
            accessRecordVo.setCarNo(carNo);
            String sfz = AESSJY.AESjiemi(accessRecordVo.getCertificatecardNo().toString());
            if (accessRecordVo.getAccessMold() == 0){
                iReservationApplicationService.updateByArrivals(sfz, DateUtil.timestampToDate(accessRecordVo.getAccessTime()));
            }

            accessRecordVo.setCertificatecardNo(AES.AESjiami(sfz));
            if (accessRecordVo.getAccessPhoto() != null && accessRecordVo.getAccessPhoto().length() > 100) {
                code = UploadUtil.upload(uploadUrl, accessRecordVo.getAccessPhoto());
                accessRecordVo.setAccessPhoto(code);
            } else {
                accessRecordVo.setAccessPhoto(null);
            }
            String now = DateUtil.timestampToString(accessRecordVo.getAccessTime());
            accessRecordVo.setAccessTime(now);
            if (accessRecordVo.getCarPhoto() != null && accessRecordVo.getCarPhoto().length() > 100) {
                String codes = UploadUtil.upload(uploadUrl, accessRecordVo.getCarPhoto());
                accessRecordVo.setCarPhoto(codes);
            } else {
                accessRecordVo.setCarPhoto(null);
            }
            //单位编号 转换成id 存储
            String institutionCode = accessRecordVo.getInstitutionCode();
            Institution institutionByCode = iInstitutionService.getInstitutionByCode(institutionCode);
            accessRecordVo.setInstitutionCode(institutionByCode.getId() + "");
            VisitorAccessre visitorAccessre = new VisitorAccessre();
            BeanUtils.copyProperties(accessRecordVo, visitorAccessre);
            //调mongo

            //当前一个月的
            baseMapper.insert(visitorAccessre);
            VisitorAccessreOld visitorAccessreOld = new VisitorAccessreOld();
            BeanUtils.copyProperties(accessRecordVo, visitorAccessreOld);
            //备份老的
            visitorAccessreOldService.insertVisitorAccessreOld(visitorAccessreOld);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return code;
    }

    @Override
    public void deleteMonthRecord() {
        String qianMonth = DateUtil.getQianXday(30);
        LambdaQueryWrapper<VisitorAccessre> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(VisitorAccessre::getId, VisitorAccessre::getAccessPhoto, VisitorAccessre::getCarPhoto);
        queryWrapper.le(VisitorAccessre::getAccessTime, qianMonth);
        queryWrapper.and(wrapper ->
                wrapper.ne(VisitorAccessre::getAccessPhoto, "").or().ne(VisitorAccessre::getAccessPhoto, ""));
        List<VisitorAccessre> list = baseMapper.selectList(queryWrapper);

        for (VisitorAccessre visitorAccessre : list) {

            if (StringUtils.isNotBlank(visitorAccessre.getAccessPhoto())) {
                String s = deletedUrl + visitorAccessre.getAccessPhoto();
                String body = HttpUtil.createGet(s).execute().body();
            }
            if (StringUtils.isNotBlank(visitorAccessre.getCarPhoto())) {
                String s = deletedUrl + visitorAccessre.getCarPhoto();
                String body = HttpUtil.createGet(s).execute().body();
            }
            VisitorAccessre visitorAccessre1 = new VisitorAccessre();
            visitorAccessre1.setId(visitorAccessre.getId());
            visitorAccessre1.setAccessPhoto("");
            visitorAccessre1.setCarPhoto("");
            baseMapper.updateById(visitorAccessre1);
        }
    }


    @Override
    public List<Object> todayCarData() {
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("message", "操作成功!");
        map.put("code", "200");
        List<Object> result = new ArrayList<>();
        String now = DateUtil.getTime();
        //周
        Map<String, Object> dt = null;
        AccessTimeVo accessTimeVo = new AccessTimeVo();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //数据权限
        String area = sysUser.getInstitutionId();


        if (StringUtils.isNotEmpty(area)) {
            if (area.length() > 0 && ",".equals(area.substring(area.length() - 1))) {
                area = area.substring(0, area.length() - 1);
            }
        } else {
            area = "";
            if (area.length() > 0 && ",".equals(area.substring(area.length() - 1))) {
                area = area.substring(0, area.length() - 1);
            }
        }
        if (StringUtils.isNotEmpty(area)) {
            accessTimeVo.setAreaCode("(" + area + ")");
        } else {
            accessTimeVo.setAreaCode("");

        }
        String nows = now.substring(0, 10);
        accessTimeVo.setSt11(nows + " 18:");
        accessTimeVo.setSt10(nows + " 07:");
        accessTimeVo.setSt9(nows + " 16:");
        accessTimeVo.setSt8(nows + " 15:");
        accessTimeVo.setSt7(nows + " 14:");
        accessTimeVo.setSt6(nows + " 13:");
        accessTimeVo.setSt5(nows + " 12:");
        accessTimeVo.setSt4(nows + " 11:");
        accessTimeVo.setSt3(nows + " 10:");
        accessTimeVo.setSt2(nows + " 09:");
        accessTimeVo.setSt1(nows + " 08:");

        accessTimeVo.setSt12(now.substring(0, 10) + " 00:00:00");
        accessTimeVo.setEnd12(now.substring(0, 10) + " 23:59:59");
        Map<String, String> map1 = visitorAccessreMapper.selectHourTimeByCar(accessTimeVo);
        Map<String, String> map2 = visitorAccessreMapper.selectHourTimeByCarOut(accessTimeVo);
        for (int i = 1; i < 12; i++) {
            dt = new HashMap<>();
            if (map1 != null) {
                dt.put("white", map1.get("t" + i));
            } else {
                dt.put("white", "0");
            }
            if (map2 != null) {
                dt.put("notWhite", map2.get("t" + i));
            } else {
                dt.put("notWhite", "0");
            }
            if (i > 2) {
                dt.put("time", (i + 7) + "" + ":00");
            } else {
                dt.put("time", "0" + (i + 7) + "" + ":00");
            }
            result.add(dt);
        }
        return result;
    }
}
